package com.hqyj.freemovie.front.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hqyj.freemovie.front.domain.Movie;
import com.hqyj.freemovie.front.json.R;
import com.hqyj.freemovie.front.service.MovieService;
import com.hqyj.freemovie.front.view.MovieView;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("movie")
public class MovieController extends BaseController {

    @Resource
    private MovieService movieService;


    @GetMapping("/catalog/{type}")
    @Cacheable(cacheNames = "movieList", key = "'type-' + #type")
    public R queryMovieListByType(@PathVariable("type") String type) {
        try {
            List<Movie> movieList = movieService.queryMovieListByType(type);
            return R.success("查询指定板块电影列表成功！", movieList);
        } catch (Exception ex) {
            ex.printStackTrace();
            return R.error("程序出现异常！");
        }
    }

    //localhost:8080/movie-front/movie/catalog/pager/爱情/1
    @GetMapping("/catalog/pager/{type}")
    public R queryMoviePagerByType(@PathVariable("type") String type, @RequestParam("currentPageNumber") Integer currentPageNumber) {
        try {
            Page<Movie> page = movieService.queryMoviePagerByType(type, currentPageNumber);
            return R.success("查询指定板块分页电影列表成功！", page);
        } catch (Exception ex) {
            ex.printStackTrace();
            return R.error("程序出现异常！");
        }
    }

    @GetMapping("/elasticsearch/{keyword}/{currentPageNumber}")
    public R elasticSearchMoviesByKeyword(@PathVariable("keyword") String keyword, @PathVariable("currentPageNumber") Integer currentPageNumber) {
        try {
            org.springframework.data.domain.Page<MovieView> movieViewPage = movieService.elasticsearchMoviesByKeyword(keyword, currentPageNumber);
            return R.success("电影关键字全文检索成功！", movieViewPage);
        } catch (Exception ex) {
            ex.printStackTrace();
            return R.error("程序出现异常！");
        }
    }
}
